/**
 * Copyright (c) 2011 Basil Shikin, VintageRadio Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.vintagephone.view.opengl.shapes;

import android.util.Log;


public class Cylinder
	extends ShapeBase
{
	private final static float PI = (float)Math.PI;
	
	private int m_slices = 40;
	private float m_radius = 3;
	private float m_height = 3;
	
	public void setRadius( float radius ) 
	{
		m_radius = radius;
	}
	
	@Override
	protected void loadShape() 
	{
		final float delta = (2*PI)/m_slices;
		float[] vertices = new float [ 3*2*m_slices ];
		
		float theta = delta;
		float x, z;		
		short ix = 0;
		for ( int i = 0; i < 2*m_slices; i += 2, ix += 6 )
		{
			x = m_radius*(float)Math.cos(theta);
			z = m_radius*(float)Math.sin(theta);
			
			Log.d("--> " + i, theta + ": (" + x + "," + z + ")");
			Log.d("--> " + i + 1, theta + ": (" + x + "," + z + ")");
			
			vertices[ix + X] = x;
			vertices[ix + Y] = 0;
			vertices[ix + Z] = z;
			
			vertices[ix + 3 + X] = x; // x
			vertices[ix + 3 + Y] = m_height; // y
			vertices[ix + 3 + Z] = z;
			
			theta += delta;
		}
		
		short[] indicies = new short[ 6*m_slices ];
		ix = 0;
		short b0 = 0;
		short b1 = 1;
		short b0n = b0;
		short b1n = 1;
		int verticiesCount = 2*m_slices;
		for ( short i = 0; i < m_slices; i += 1, ix += 6 )
		{
			b0n = (short)((b0 + 2) % verticiesCount);
			b1n = (short)((b1 + 2) % verticiesCount);
			
			indicies[ ix + 0 ] = b0n;
			indicies[ ix + 1 ] = b0;
			indicies[ ix + 2 ] = b1;
			indicies[ ix + 3 ] = b0n;
			indicies[ ix + 4 ] = b1n;
			indicies[ ix + 5 ] = b1;
			
			b0 = b0n;
			b1 = b1n;
			
			Log.d("--> " + i, "[" + indicies[ ix + 0 ] + ", " + indicies[ ix + 1 ] + ", " + indicies[ ix + 2 ] + "]" +
						      "[" + indicies[ ix + 3 ] + ", " + indicies[ ix + 4 ] + ", " + indicies[ ix + 5 ] + "]" );
		}
		
		
		setVertices(vertices);
		setIndices( indicies );
	}

	
	
}
